package org.scau.studentevaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.scau.studentevaluation.entity.Appeal;
import org.scau.studentevaluation.entity.ExamAct;
import org.scau.studentevaluation.entity.ExamOtherAct;
import org.scau.studentevaluation.entity.Examin;
import org.scau.studentevaluation.entity.dos.ActDO;
import org.scau.studentevaluation.entity.dos.ExamDO;
import org.scau.studentevaluation.entity.dos.OtherActDO;
import org.scau.studentevaluation.service.IExamServicePart1;
import org.scau.studentevaluation.service.ImportMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.Calendar;
import java.util.List;

/**
 * Created by admin on 2020/8/26.
 */
@Service
// service层不需要标注@XXXMapping
public class ExamServicePart1Impl implements IExamServicePart1 {

    @Autowired
    private ImportMapperService importMapperService;
    // lyl：spring容器自动注入ExamServicePart2Impl实例，无需自己实例化 【new ExamServicePart2Impl()】
    @Autowired
    private ExamServicePart2Impl examServicePart2;

    @Override
    public List<ExamAct> hello(String userName) {

        return examServicePart2.getActList(userName);



    }



    public List<ExamOtherAct> helloOther(String college){

        return examServicePart2.getOtherActList(college);

    }

    @Override
    public int examin(Examin examin) {
        return exam(examin);
    }

    @Transactional
    public int exam(Examin examin) {
        System.out.println(examin.getCollege()+" 审核 " + examin.getFrom() +"  " + examin.getId());
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        if (calendar.get(Calendar.MONTH) < 10)year--;
        if(examin.getFrom().equals("社团")){
            //这是证明端的审核
            //通过actId找到活动
            int actId = examin.getId();//获取actId
            //todo
            //判断是否通过
            if (examin.isPass()){
                //通过
                System.out.println(actId + "通过");
                int subject = examin.getSubject();
                int reluIndex = examin.getIndex();
                float mark = examin.getMark();
                String college = examin.getCollege();
                // passAct()public int updateExam(boolean passed,boolean examed,boolean appealed,int reluIndex,int subject,int reluYear,float mark,String college,String appealContent,String appealReply);
//                importMapperService.indexMapper.updateExam(true,true,false,reluIndex,subject,year,mark,college,"","",actId);//社团不开放申诉

                // lyl: 有逻辑错误，subject、ruleIndex、mark在未审核时是null值，不可能作为where的筛选条件
                LambdaUpdateWrapper<ExamDO> update = new LambdaUpdateWrapper<ExamDO>();
                        update.eq(ExamDO::getActId,actId).eq(ExamDO::getCollege,college)
                        .set(ExamDO::getIsPassed,true).set(ExamDO::getIsExamed,true).set(ExamDO::getIsAppealed,false).set(ExamDO::getRuleYear,year)
                        .set(ExamDO::getAppealContent,"").set(ExamDO::getAppealReply,"").set(ExamDO::getActId,actId);

                importMapperService.examMapper.update(null,update); // lyl:执行更新


            }
            else {
                System.out.println(actId + "不通过");
                //不通过
                //noPassAct()
                String college = examin.getCollege();
//                importMapperService.indexMapper.updateExam(false,true,false,-1,-1,-1,-1,college,"","",actId);
                LambdaUpdateWrapper<ExamDO> update = new LambdaUpdateWrapper<ExamDO>();
                    update.eq(ExamDO::getCollege,college).eq(ExamDO::getActId,actId)
                            .set(ExamDO::getIsPassed,false).set(ExamDO::getIsExamed,true).set(ExamDO::getIsAppealed,false).set(ExamDO::getRuleIndex,-1)
                            .set(ExamDO::getSubject,-1).set(ExamDO::getRuleYear,-1).set(ExamDO::getMark,-1)
                            .set(ExamDO::getAppealContent,"").set(ExamDO::getAppealReply,"").set(ExamDO::getActId,actId);
                importMapperService.examMapper.update(null,update); // lyl:执行更新

            }

        }
        else if (examin.getFrom().equals("个人")){
            //获取id
            int oacId = examin.getId();
            if (examin.isPass()){
                System.out.println(examin.getId() + "通过");
                // 通过
                //操作exam表：
                int subject = examin.getSubject();
                int reluIndex = examin.getIndex();
                float mark = examin.getMark();
                String college = examin.getCollege();
//                importMapperService.indexMapper.updateExamOther(true,true,false,reluIndex,subject,year,mark,college,"","",oacld);
                LambdaUpdateWrapper<ExamDO> update1 = new LambdaUpdateWrapper<ExamDO>();
                // lyl：同上删除三个eq条件
                update1.eq(ExamDO::getCollege,college).eq(ExamDO::getActId,oacId)
                        .set(ExamDO::getIsPassed,true).set(ExamDO::getIsExamed,true).set(ExamDO::getIsAppealed,false)
                        .set(ExamDO::getRuleYear,year).set(ExamDO::getAppealContent,"").set(ExamDO::getAppealReply,"");
                importMapperService.examMapper.update(null,update1); // lyl:执行update1
                //操作otheract表
//                importMapperService.indexMapper.updateOther(true,true,oacld);
                // lyl：删除一个eq条件
                LambdaUpdateWrapper<OtherActDO> update2 = new LambdaUpdateWrapper<OtherActDO>();
                update2.eq(OtherActDO::getCollege,college).eq(OtherActDO::getId,oacId)
                        .set(OtherActDO::getIsChecked,true).set(OtherActDO::getIsPassed,true).set(OtherActDO::getId,oacId);
                importMapperService.otherActMapper.update(null,update2); // lyl:执行update2
            }
            else {
                System.out.println(examin.getId() + "不通过");
                String college = examin.getCollege();
//                importMapperService.indexMapper.updateExamOther(false,true,false,-1,-1,-1,-1,college,"","",oacld);
                LambdaUpdateWrapper<ExamDO> update1 = new LambdaUpdateWrapper<ExamDO>();
                update1.eq(ExamDO::getCollege,college).eq(ExamDO::getActId,oacId)
                        .set(ExamDO::getIsPassed,false).set(ExamDO::getIsExamed,true).set(ExamDO::getIsAppealed,false)
                        .set(ExamDO::getRuleIndex,-1).set(ExamDO::getSubject,-1).set(ExamDO::getRuleYear,-1).set(ExamDO::getMark,-1)
                        .set(ExamDO::getAppealContent,"").set(ExamDO::getAppealReply,"");
                importMapperService.examMapper.update(null,update1); // lyl: 执行update1
                //操作otheract表
//                importMapperService.indexMapper.updateOther(true,false,oacld);
                LambdaUpdateWrapper<OtherActDO> update2 = new LambdaUpdateWrapper<OtherActDO>();
                update2.eq(OtherActDO::getCollege,college).eq(OtherActDO::getId,oacId)
                        .set(OtherActDO::getIsChecked,true).set(OtherActDO::getIsPassed,false).set(OtherActDO::getId,oacId);
                importMapperService.otherActMapper.update(null,update2); //lyl:执行update2
            }
        }
        return 1;
    }

    public int deleteAct( String college,String from,int id){

        if (from.equals("社团")){
            //社团
            //通过actId删除
//            indexMapper.deleteAct(id,true);
//            LambdaQueryWrapper<ActDO> query1 = new LambdaQueryWrapper<ActDO>();
//            query1.eq(ActDO::getId,id);
//            importMapperService.actMapper.delete(query1);
            LambdaUpdateWrapper<ExamDO> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ExamDO::getActId,id)
                    .set(ExamDO::getIsDeleted,true);
            importMapperService.examMapper.update(null,updateWrapper);
        }
        else {
            //个人
            //通过otherId删除
//            indexMapper.deleteOtherAct(id,true);
//            LambdaQueryWrapper<OtherActDO> query2 = new LambdaQueryWrapper<OtherActDO>();
//            query2.eq(OtherActDO::getId,id);
//            importMapperService.otherActMapper.delete(query2);
            LambdaUpdateWrapper<ExamDO> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ExamDO::getOtherActId,id)
                    .set(ExamDO::getIsDeleted,true);
            importMapperService.examMapper.update(null,updateWrapper);
        }
        return 1;
    }

    public int getAppeal(@RequestBody Appeal appeal){
        System.out.println(appeal);
        ExamServicePart1Impl ep2 = new ExamServicePart1Impl();
        ep2.appeal(appeal);
        return 1;
    }

    public int appeal(Appeal appeal){
        if (appeal.getFrom().equals("个人")){
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int oacId = appeal.getId();
            if (appeal.isPass()){
                //通过
                //操作exam表 appealReply
//                indexMapper.updateExamByappeal(oacId,appeal.getReply());
                LambdaUpdateWrapper<ExamDO> update1 = new LambdaUpdateWrapper<ExamDO>();
                update1.eq(ExamDO::getId,oacId)
                        .set(ExamDO::getAppealReply,appeal.getReply());
                //操作otheract表 布尔值
//                indexMapper.updateOtherByappeal(oacId,true,true);
                LambdaUpdateWrapper<OtherActDO> update2 = new LambdaUpdateWrapper<OtherActDO>();
                update2.eq(OtherActDO::getId,oacId)
                        .set(OtherActDO::getIsChecked,true).set(OtherActDO::getIsPassed,true);
            }
            else {
                //未通过
                //操作exam表 appealReply
//                indexMapper.updateExamByappeal(oacId,appeal.getReply());
                LambdaUpdateWrapper<ExamDO> update1 = new LambdaUpdateWrapper<ExamDO>();
                update1.eq(ExamDO::getId,oacId)
                        .set(ExamDO::getAppealReply,appeal.getReply());
                //操作otheract表 布尔值
//                indexMapper.updateOtherByappeal(oacId,true,false);
                LambdaUpdateWrapper<OtherActDO> update2 = new LambdaUpdateWrapper<OtherActDO>();
                update2.eq(OtherActDO::getId,oacId)
                        .set(OtherActDO::getIsChecked,true).set(OtherActDO::getIsPassed,false);
            }
        }
        else if (appeal.getFrom().equals("社团") ){
            System.out.println("社团申诉暂未开放");
        }
        return 1;
    }
}
